﻿/*******************************************************************  
 * Copyright (C) vancl.com
 * All rights reserved. 
 * 
 * Created By: VANCLOA\hanxianlong
 * Create Date:2011/8/17 11:47:11
 * Last Modified By:VANCLOA\hanxianlong
 * Last Modified Date:2011/8/17 11:47:11
 * Description: 请添加对类文件的描述
 * ********************************************************************/
#region Using directives
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using SimpleCodeGenerator.Schema;
using SimpleCodeGenerator.Utility;
#endregion

namespace SimpleCodeGenerator.Generators
{
	/// <summary>
	/// Model生成器
	/// </summary>
	public class ModelGenerator : GeneratorBase
	{
		public override void Generate(TableSchema table)
		{
			MyStringBuilder sResult = new MyStringBuilder();

			#region copyright/using block
			BuildCopyRight(sResult);
			BuildCommonUsingBlock(sResult);
			sResult.AppendLine(); 
			#endregion

			string modelName = table.Code + "Info";

			#region Namespace
			sResult.AppendFormat("namespace Vancl.CMS.Model{0}", Namespace).AppendLine();
			sResult.AppendLine("{"); 
			#endregion

			#region class signature
			sResult.IncreaseIndent();//increate first time
			base.BuildRemark(sResult, table.Comment + "的实体类");

			sResult.AppendFormat("public class {0}", modelName).AppendLine();

			sResult.AppendLine("{");//class start 
			#endregion

			sResult.IncreaseIndent();//increase for properties

			//generate properties for this class
			GenerateProperties(sResult, table.Columns);

			sResult.DecreaseIndent();//decreate for properties

			sResult.AppendLine("}");//class end
			sResult.DecreaseIndent();//decreate first time

			sResult.AppendLine( "}");//namespace end

			Save(modelName, sResult.ToString());
		}

		/// <summary>
		/// 生成类的属性
		/// </summary>
		/// <param name="output">生成的字符串要输出到的stringbuilder</param>
		/// <param name="props">列的elements集合</param>
		private void GenerateProperties(MyStringBuilder output,List<ColumnSchema> columns)
		{
			foreach (var column in columns)
			{
				BuildRemark(output, column.Comment);
				output.AppendFormat("public {0} {1} {{ get; set; }}", column.DataType, column.Code)
					.AppendLine();
				output.AppendLine();
			}
		}
	}
}